package com.hudongwx.godoffood.activity.Shopping_Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.hudongwx.godoffood.R;
import com.hudongwx.godoffood.utils.DBUtils.Address;
import com.hudongwx.godoffood.utils.DBUtils.DbOpenUtils;
import com.hudongwx.godoffood.utils.DBUtils.ShoppingCar;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_confirm)
public class ConfirmActivity extends AppCompatActivity {
    private String TAG = "ConfirmActivity";
    @ViewInject(R.id.confirm_recycleView)
    private RecyclerView recyclerView;
    @ViewInject(R.id.confirm_text_1)
    private TextView textView1;
    @ViewInject(R.id.confirm_text_2)
    private TextView textView2;
    @ViewInject(R.id.confirm_text_3)
    private TextView textView3;
    @ViewInject(R.id.confirm_text_4)
    private TextView textView4;
    @ViewInject(R.id.confirm_textView_5)
    private TextView textView5;
    @ViewInject(R.id.confirm_tv_1)
    private TextView textView6;
    @ViewInject(R.id.confirm_tv_4)
    private TextView textView7;
    @ViewInject(R.id.confirm_tv_2)
    private TextView textView8;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        date();
    }

    //数据的加载和设配起
    private void date() {
        Intent intent = getIntent();
        String text = intent.getStringExtra("money");
        String mySum = intent.getStringExtra("Mysum");
        textView8.setText(mySum + "" + "件");
        textView6.setText(text + "");
        textView7.setText(text);
        add();
        //获取传递过来的数组
        Object[] cobjs = (Object[]) intent.getSerializableExtra("date");
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true);
        recyclerView.setLayoutManager(layoutManager);
        RecycleViewAdapter adapter = new RecycleViewAdapter(cobjs);
        recyclerView.setAdapter(adapter);
    }

    /**
     * 地址的跳转
     *
     * @param view
     */
    @Event(R.id.confirm_frameLayout_1)
    private void frame(View view) {
        Intent intent = new Intent(ConfirmActivity.this, NumberActivity.class);
        startActivityForResult(intent, 1);
    }

    /**
     * 配送的跳转
     *
     * @param view
     */
    @Event(R.id.confirm_relativeLayout_1)
    private void relative_1(View view) {

        Intent intent = new Intent(ConfirmActivity.this, DistributionActivity.class);
        startActivityForResult(intent, 2);
    }

    /**
     * 发票的跳转
     *
     * @param view
     */
    @Event(R.id.confirm_relativeLayout_2)
    private void relative_2(View view) {

        Intent intent = new Intent(ConfirmActivity.this, InvoiceActivity.class);
        String text = (String) textView5.getText();
        startActivityForResult(intent, 3);
    }

    @Event(R.id.confirm_bu_1)
    private void button(View view) {

    }

    /**
     * 返回按钮
     *
     * @param view
     */
    @Event(R.id.confirm_img_1)
    private void onclik(View view) {
        finish();
    }

    /*
    适配器
     */
    class RecycleViewAdapter extends RecyclerView.Adapter<ViewHolder> {
        Object[] obj;

        public RecycleViewAdapter(Object[] obj) {
            this.obj = obj;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(ConfirmActivity.this).inflate(R.layout.recyclerviewholder_confirm, null);
            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            if (obj.length != 0) {

                ShoppingCar car = (ShoppingCar) obj[position];
                x.image().bind(holder.img, car.getImg());
                holder.name.setText(car.getName() + "");
                holder.pirce.setText(car.getPrice() + "");
                holder.num.setText(car.getNum() + "");
                holder.type.setText(car.getType() + "");

            }
        }


        @Override
        public int getItemCount() {
            return obj.length;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView name, type, num, pirce;
        ImageView img;

        public ViewHolder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.recycle_shopping_img_1);
            name = (TextView) itemView.findViewById(R.id.recycle_shopping_tv_1);
            type = (TextView) itemView.findViewById(R.id.recycle_shopping_tv_2);
            num = (TextView) itemView.findViewById(R.id.recycle_shopping_tv_4);
            pirce = (TextView) itemView.findViewById(R.id.recycle_shopping_tv_6);
        }
    }

    /**
     * 在Activity中返回的数据
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 1:
                add();
                break;
            //配送的返回数据
            case 2:
                String distribution = data.getStringExtra("distribution");
                textView4.setText(distribution);
                break;
            case 3:
                //发票的返回数据
                String minmi = data.getStringExtra("xinxi");
                String on = data.getStringExtra("on");
                Log.i(TAG, "onActivityResult: "+minmi+on);
                if (minmi==null){
                    textView5.setText("不开发票");
                }else {
                    textView5.setText(minmi + on);
                }

                break;
        }
    }

    /**
     * 在numberActivity中回访的数据
     */
    private void add() {
        DbManager dbManager = DbOpenUtils.getShoppingDb2();

        try {
            List<Address> all = dbManager.findAll(Address.class);
            if (all!=null){
            for (int i = 0; i < all.size(); i++) {
                if (all.get(i).getOne()) {
                    String name = all.get(i).getName();
                    String address_1 = all.get(i).getAddress_1();
                    String address_2 = all.get(i).getAddress_2();
                    String call = all.get(i).getCall();
                    Log.i(TAG, "add: " + name + address_1 + address_2 + call);
                    textView1.setText("姓名:" + name);
                    textView2.setText("电话:" + call);
                    textView3.setText(address_1 + address_2 + "");
                }
            }
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

}